package com.tuyue.police.controller.vip;

import com.tuyue.police.bean.DeviceBean;
import com.tuyue.police.biz.IDeviceBiz;
import com.tuyue.police.biz.ISectorBiz;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.Device;
import com.tuyue.police.util.Result;
import com.tuyue.police.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/14.
 * @Modified By:
 */
@RestController
@RequestMapping("vip/device")
@Slf4j
public class DeviceController {

    @Autowired
    private IDeviceBiz deviceBiz;

    @Autowired
    private ISectorBiz sectorBiz;

    /**
     * 新增设备和防区
     * @param deviceBean
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody DeviceBean deviceBean){
        log.info(deviceBean.toString());
        if (StringUtils.isEmpty(deviceBean.getDeviceEnterpriseId())) {
            throw new MyException(ResultEnum.ENTERPRISE_NOTNULL);
        }
        DeviceBean bean = deviceBiz.add(deviceBean);
        return ResultUtil.success(bean);
    }

    /**
     * 通过用户ID获取 设备列表(一级列表)
     * @param userId
     * @return
     */
    @GetMapping("list/{userId}")
    public Result list(@PageableDefault(size = 10, sort = {"deviceId"}, direction = Sort.Direction.DESC)
                                   Pageable pageable, @PathVariable Long userId){
        Page<Device> devices=deviceBiz.findByUserId(userId,pageable);
        return ResultUtil.success(devices);
    }

    /**
     * 通过设备ID查询设备详情
     * @param deviceId
     * @return
     */
    @GetMapping("findById/{deviceId}")
    public Result findById(@PathVariable Long deviceId){
        DeviceBean device=deviceBiz.findById(deviceId);
        return ResultUtil.success(device);
    }
//    @GetMapping("delSector/{sId}")
//    public Result delSector(@PathVariable Long sId){
//        sectorBiz.delSector(sId);
//        return ResultUtil.success();
//    }



}
